package cn.yunyichina.reservation.controller.business;

import cn.yunyichina.provider.framework.dubbo.service.ReservationService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.reservation.common.ResponseEnum;
import cn.yunyichina.reservation.common.jackson.Jackson;
import cn.yunyichina.reservation.constant.HttpResponse;
import cn.yunyichina.reservation.constant.Interface;
import cn.yunyichina.reservation.vo.CityVo;
import cn.yunyichina.reservation.vo.ProvinceVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lullaby on 2016/8/24.
 */
@RestController
@RequestMapping("common")
public class BizController {

    private static final String RESERVATION_CONSOLE_PROVINCES_GET = "yyt.reservation.province.get";

    private static final String RESERVATION_CONSOLE_CITY_GET = "yyt.reservation.city.get";

    @GetMapping("provinces")
    public HttpResponse getProvinces() {
        HttpResponse httpResponse = new HttpResponse();
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_PROVINCES_GET, null
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.FAILURE.getCode(), ResponseEnum.FAILURE.getMessage());
        }
        if (Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            List<ProvinceVo> provinces = Jackson.getBeanJsonData(serviceResponse.getResult(), List.class);
            httpResponse.setData(provinces);
        } else {
            httpResponse.failure(serviceResponse.getResultCode(), serviceResponse.getResultMessage());
        }
        return httpResponse;
    }

    @GetMapping("province/cities")
    public HttpResponse getCities(HttpServletRequest request) {
        String provinceId = request.getParameter("province_id");
        HttpResponse httpResponse = new HttpResponse();
        if (StringUtils.isBlank(provinceId)) {
            return httpResponse.failure(ResponseEnum.ILLEGAL_PARAMETER.getCode(), ResponseEnum.ILLEGAL_PARAMETER.getMessage());
        }
        Map<String, String> params = new HashMap<>();
        params.put("province_id", provinceId);
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_CITY_GET, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.FAILURE.getCode(), ResponseEnum.FAILURE.getMessage());
        }
        if (Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            List<CityVo> cities = Jackson.getBeanJsonData(serviceResponse.getResult(), List.class);
            httpResponse.setData(cities);
        } else {
            httpResponse.failure(serviceResponse.getResultCode(), serviceResponse.getResultMessage());
        }
        return httpResponse;
    }

}
